﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Media;
using GalaSoft.MvvmLight.Command;

namespace Songhay.Silverlight.BiggestBox.IndexPart.ViewModels
{
    using Songhay.Mvvm.ViewModels;

    /// <summary>
    /// View Model for LayoutStates sample.
    /// </summary>
    /// <remarks>
    /// This type is based on the gallery sample “Dynamic Layout and Transitions in Blend 4”
    /// by Kenny Young, Expression Blend Architect
    /// [http://gallery.expression.microsoft.com/DynamicLayoutTrans/]
    /// </remarks>
    public class KennyLayoutStatesViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="KennyLayoutStatesViewModel"/> class.
        /// </summary>
        /// <remarks>
        /// For details, see
        /// “How to: Implement Property Change Notification”
        /// http://msdn.microsoft.com/en-us/library/ms743695.aspx
        /// A sample <c>set</c> block:
        /// <code>
        /// set
        /// {
        /// _myProperty = value;
        /// OnPropertyChanged("MyProperty");
        /// }
        /// </code>
        /// </remarks>
        public KennyLayoutStatesViewModel()
        {
            this._colors = new ObservableCollection<KennyColorViewModel>();
            this._r = new Random(DateTime.Now.Second);

            #region commanding:

            this.ColorAddCommand = new RelayCommand(
                () =>
                {
                    var color = new KennyColorViewModel(
                        Color.FromArgb(255,
                            (byte)this._r.Next(256),
                            (byte)this._r.Next(256),
                            (byte)this._r.Next(256)));
                    this.Colors.Insert(this._r.Next(this.Colors.Count + 1), color);
                });

            this.ColorRemoveCommand = new RelayCommand(
                () =>
                {
                    if(this.Colors.Count == 0) return;
                    this.Colors.RemoveAt(this._r.Next(this.Colors.Count));
                });

            #endregion

            if(!this.IsInDesignMode) return;

            this.Colors.Add(new KennyColorViewModel("#FF8DD3C7"));
            this.Colors.Add(new KennyColorViewModel("#FFFFFFB3"));
        }

        /// <summary>
        /// Gets the colors.
        /// </summary>
        /// <value>The colors.</value>
        public ObservableCollection<KennyColorViewModel> Colors { get { return this._colors; } }

        /// <summary>
        /// Gets or sets the color-add command.
        /// </summary>
        /// <value>The color-add command.</value>
        public RelayCommand ColorAddCommand { get; private set; }

        /// <summary>
        /// Gets or sets the color-remove command.
        /// </summary>
        /// <value>The color-remove command.</value>
        public RelayCommand ColorRemoveCommand { get; private set; }

        ObservableCollection<KennyColorViewModel> _colors;
        Random _r;
    }
}
